﻿#include "Str2unicodeutf8.h"
#include <QTextEdit>
#include <QLabel>
#include <QtEndian>
Str2UnicodeUtf8::Str2UnicodeUtf8(QWidget *parent) : QWidget(parent)
{
    QHBoxLayout *h_top = new QHBoxLayout();
    QHBoxLayout *h_middle = new QHBoxLayout();

    QHBoxLayout *h_bottom = new QHBoxLayout();
    QVBoxLayout *v_left_bottom = new QVBoxLayout();
    QVBoxLayout *v_right_bottom = new QVBoxLayout();

    QVBoxLayout *v_main = new QVBoxLayout();

    QRadioButton *str2unicodeBig = new QRadioButton("Str2UnicodeB", this);
    QRadioButton *str2unicodeLittle = new QRadioButton("Str2UnicodeL", this);
    QRadioButton *str2utf8 = new QRadioButton("Str2Utf8", this);
    str2utf8->setChecked(true);

    QLabel *input_label = new QLabel("文本字符串",this);
    QLabel *output_label = new QLabel("UTF字符串",this);

    QTextEdit *input_text = new QTextEdit(this);
    QTextEdit *output_text = new QTextEdit(this);
    output_text->setReadOnly(true);
    m_switchBtn = new SwitchButton(this);
    m_switchBtn->setTextOn(QString("U2Str"));
    m_switchBtn->setTextOff(QString("Str2U"));
    m_switchBtn->setMinimumSize(100,20);
    QPushButton *startBtn = new QPushButton("开始转换",this);

    h_top->addStretch(0);
    h_top->setSpacing(5);
    h_top->addWidget(str2utf8);
    h_top->addWidget(str2unicodeLittle);
    h_top->addWidget(str2unicodeBig);
    h_top->addStretch(0);

    h_middle->addStretch(0);
    h_middle->addWidget(m_switchBtn);
    h_middle->addSpacing(100);
    h_middle->addWidget(startBtn);
    h_middle->addStretch(0);

    v_left_bottom->addWidget(input_label);
    input_label->setAlignment(Qt::AlignCenter);
    v_left_bottom->addWidget(input_text);
    v_left_bottom->setContentsMargins(6, 6, 0, 0);

    v_right_bottom->addWidget(output_label);
    output_label->setAlignment(Qt::AlignCenter);
    v_right_bottom->addWidget(output_text);
    v_right_bottom->setContentsMargins(0, 6, 6, 0);

    h_bottom->addLayout(v_left_bottom);
    h_bottom->addLayout(v_right_bottom);

    v_main->addLayout(h_top);
    v_main->addLayout(h_middle);
    v_main->addLayout(h_bottom);
    v_main->setMargin(0);
    this->setLayout(v_main);
    str2unicodeBig->setProperty("class", "str2utf_radiobtn");
    str2unicodeLittle->setProperty("class", "str2utf_radiobtn");
    str2utf8->setProperty("class", "str2utf_radiobtn");

    startBtn->setObjectName("str2utf_btn_start");
    input_label->setObjectName("str2utf_label_input");
    output_label->setObjectName("str2utf_label_output");
    input_text->setObjectName("str2utf_text_input");
    output_text->setObjectName("str2utf_text_output");
    connect(m_switchBtn, &SwitchButton::statusChanged, this, [input_label, output_label, input_text, output_text,this](bool status){
        if (!status){
            input_label->setText("文本字符串");
            output_label->setText("UTF字符串");
        }else {
            output_label->setText("文本字符串");
            input_label->setText("UTF字符串");
        }
        input_text->clear();
        output_text->clear();
    });
    connect(startBtn, &QPushButton::clicked, this, [input_text, output_text, str2unicodeBig, str2unicodeLittle, str2utf8, this]{
        if (!input_text->toPlainText().isEmpty()){
            QString desStr, str;
            ushort ret;
            bool isStr2Utf = true;
            if (!m_switchBtn->checked()){
                isStr2Utf = true;
            }else {
                isStr2Utf = false;
            }
            QString input_data = input_text->toPlainText();
            if (str2unicodeBig->isChecked()){
                if (isStr2Utf){
                    for (int i = 0; i<input_data.count();i++){
                        ret = qToBigEndian(input_data.at(i).unicode());
                        desStr += str.setNum(ret,16);
                    }
                    output_text->setText(desStr.toUpper());
                }else {
                    for (int i = 0; i < input_data.length();){
                        QString str = input_data.mid(i, 4);
                        ret = qToBigEndian(str.toUShort(0, 16));
                        i += 4;
                        desStr.append(ret);
                    }
                    output_text->setText(desStr);
                }
            }else if (str2unicodeLittle->isChecked()){
                if (isStr2Utf){
                    for (int i = 0; i<input_data.count();i++){
                        ret = input_data.at(i).unicode();
                        desStr += str.setNum(ret,16);
                    }
                    output_text->setText(desStr.toUpper());
                }else {
                    for (int i = 0; i < input_data.length();){
                        QString str = input_data.mid(i, 4);
                        ret = str.toUShort(0, 16);
                        i += 4;
                        desStr.append(ret);
                    }
                    output_text->setText(desStr);
                }
            }else if (str2utf8->isChecked()){
                if (isStr2Utf){
                    desStr = QString(input_data.toUtf8().toHex());
                    output_text->setText(desStr.toUpper());
                }else {
                    output_text->setText(QString(QByteArray::fromHex(input_data.toUtf8())));
                }
            }
        }
    });
}
